package com.lsl.bhllserver.service.user;

import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.lsl.bhllserver.entity.*;
import com.lsl.bhllserver.entity.userabstract.User;
import com.lsl.bhllserver.service.func.IAddUser;
import com.lsl.bhllserver.service.func.IDeleteUser;
import com.lsl.bhllserver.service.func.IEditUser;

@Service
public class UserManageService extends UserService{
    // public addUser(IAddUser addUser) {
    //     if addUser.adduser(customer) {
    // }

    @CacheEvict(value = "allUser", allEntries = true)
    public User addUser(IAddUser au) {
        User usertemp = au.adduser();
        if (usertemp instanceof Customer) {
            customerDAO.insert((Customer)usertemp);
            return usertemp;
        } else if (usertemp instanceof Business) {
            businessDAO.insert((Business)usertemp);
            return usertemp;
        } else {
            System.out.println("+++++ 类型不对？？？？");
            return null;
        }
    }

    @CacheEvict(value = "allUser", allEntries = true)
    public boolean deleteUser(IDeleteUser du) {
        User usertemp = du.deleteUser();
        if (usertemp instanceof Customer) {
            customerDAO.setTypeflag(new Customer());
            customerDAO.delete(((Customer)usertemp).getId());
            return true;
        } else if (usertemp instanceof Business) {
            businessDAO.setTypeflag(new Business());
            businessDAO.delete(((Business)usertemp).getId());
            return true;
        } else {
            System.out.println("+++++ 类型不对？？？？");
            return false;
        }
        //return false;
    }

    @CacheEvict(value = "allUser", allEntries = true)
    public boolean editUser(IEditUser eu) {
        //TODO: 这里的逻辑都乱了，需要全部重新写过
        //TODO: 判断用户需要修改什么应该从密码是否为空和用户名是否为空进行判断，而不是单单通过密码
        User usertemp = eu.editUser();
        if (usertemp instanceof Customer) {
            Customer cusTemp = (Customer)usertemp;
            if(cusTemp.getCusPassword() == null || cusTemp.getCusPassword().isEmpty()) { // 密码为空，则不修改密码
                cusTemp.setCusPassword(customerDAO.findByID(cusTemp.getId().toString()).getCusPassword());
                System.out.println(cusTemp.getCusPassword().toString());
                return customerDAO.update(cusTemp.getId(), cusTemp);
            }
            if(cusTemp.getCusName() == null || cusTemp.getCusName().isEmpty()) { // 用户名为空，则仅修改密码
                Customer cusTemp2 = customerDAO.findByID(cusTemp.getId().toString());
                cusTemp2.setCusPassword(cusTemp.getCusPassword());
                return customerDAO.update(cusTemp2.getId(), cusTemp2);
            }
            // 用户名和密码都不为空，则修改用户名和密码
            System.out.println(cusTemp.toString());
            return customerDAO.update(cusTemp.getId(), cusTemp);
        } else if (usertemp instanceof Business) {
            Business busTemp = (Business)usertemp;
            if(busTemp.getBusCaption() == null || busTemp.getBusCaption().isEmpty()) { // 描述为空，则不修改描述（通用前置）
                busTemp.setBusCaption(businessDAO.findByID(busTemp.getId().toString()).getBusCaption());
                //System.out.println("描述为空：获取到：" + busTemp.getBusCaption().toString());
                //return businessDAO.update(busTemp.getId(), busTemp);
            }
            if(busTemp.getBusPassword() == null || busTemp.getBusPassword().isEmpty()) { // 密码为空，则不修改密码
                busTemp.setBusPassword(businessDAO.findByID(busTemp.getId().toString()).getBusPassword());
                System.out.println("密码为空：获取到：" + busTemp.getBusPassword().toString());
                return businessDAO.update(busTemp.getId(), busTemp);
            }
            if(busTemp.getBusName() == null ||  busTemp.getBusName().isEmpty()) { // 用户名为空，则仅修改密码码
                Business busTemp2 = businessDAO.findByID(busTemp.getId().toString());
                busTemp2.setBusPassword(busTemp.getBusPassword());
                System.out.println("用户名为空：获取到密码：" + busTemp2.getBusPassword().toString());
                return businessDAO.update(busTemp2.getId(), busTemp2);
            }
            // 用户名和密码都不为空，则修改用户名和密码
            return businessDAO.update(busTemp.getId(), busTemp);
        } else if (usertemp instanceof Admin) {
            Admin adminTemp = (Admin)usertemp;
            return adminDAO.update(adminTemp.getId(), adminTemp);
        } else {
            System.out.println("+++++ 类型不对？？？？");
            return false;
        }
    }

    // 新增 findUser 方法, 用于查找用户
    // @Cacheable(value = "finduser", key = "#userid+'-'+#role")
    public User findUser(String userid, String role) {
        if(role.equals("customer")) {
            customerDAO.setTypeflag(new Customer());
            return customerDAO.findByID(userid);
        } else if (role.equals("business")) {
            businessDAO.setTypeflag(new Business());
            return businessDAO.findByID(userid);
        } else if (role.equals("admin")) {
            adminDAO.setTypeflag(new Admin());
            return adminDAO.findByID(userid);
        } else {
            System.out.println("+++++ 类型不对？？？？");
            return null;
        }
        //return null;
    }

    // @Cacheable(value = "user", key = "#keywords+'-'+#role")
    public List<? extends User> searchUser(String[] keywords, String role) {
        if(role.equals("customer")) {
            System.out.println(customerDAO.findByKeywords(keywords).toString());
            customerDAO.setTypeflag(new Customer());
            return customerDAO.findByKeywords(keywords);
        } else if (role.equals("business")) {
            System.out.println(businessDAO.findByKeywords(keywords).toString());
            businessDAO.setTypeflag(new Business());
            return businessDAO.findByKeywords(keywords);
        } else {
            System.out.println("+++++ 类型不对？？？？");
            return null;
        }
        //return null;
    }

    @Cacheable(value = "allUser", key = "#role")
    public List<? extends User> getAllUser(String role) {
        if(role.equals("customer")) {
            customerDAO.setTypeflag(new Customer());
            return customerDAO.getAll();
        } else if (role.equals("business")) {
            businessDAO.setTypeflag(new Business());
            return businessDAO.getAll();
        } else {
            System.out.println("+++++ 类型不对？？？？");
            return null;
        }
        //return null;
    }
}
